/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.camelspit.preludetoarmageddon.data;
import com.camelspit.preludetoarmageddon.entity.*;
import com.camelspit.preludetoarmageddon.util.*;

import java.util.StringTokenizer;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;

/**
 *
 * @author DMK
 */
public class ItemDatabase extends EntityVectorDatabase<Item>{
    
    private final int RECORD_LENGTH = 6 + Attributes.length;

    public ItemDatabase(){
    }
    /**
     * load() - Loads a file named "items/items.itm"
     * Each item is 18 separate strings as follows
     *
     * EXAMPLE ITEM
     * [ID]           1
     * [NAME]         Knife
     * [TYPE]         Weapon
     * [MIN]          2
     * [MAX]          4
     * [SPEED]        2
     * [STAMINA]      0
     * [STRENGTH]     0
     * [DEXTERITY]    0
     * [INTELLIGENCE] 0
     * [HEALTH]       0
     * [MANA]         0
     * [ENDURANCE]    0
     * [FORTITUDE_P]  0
     * [FORTITUDE_M]  0
     * [DODGE]        0
     * [ACCURACY_P]   0
     * [ACCURACY_M]   0
     *
     *
     * <p>There are three types of Items. Each item is given 1000
     * slots for the ID number.
     * Weapons: 1-999
     * ARMOR: 1000-1999
     * POTIONS:2000-2999
     * @return true if loaded, false if load failed
     */
    public boolean load() {
        //function declarations (not needed after load)
        File file;
        String[] record = new String[RECORD_LENGTH];
        Scanner input;
        //create a new File object for items.itm
        file = new File(UtilLib.getFileLocURI(this,
                "/com/camelspit/preludetoarmageddon/data/items/items.itm"));

        //init scanner to file
        try{
            input = new Scanner(file);
        }
        catch(FileNotFoundException e){
            return false;
        }
        //iterator for the item the loop is at
        int itemItr = 0;
        //iterator for the stat the loop is populating
        int attrItr = 0;

        boolean done = false;
        //FillEntities
        while(!done) {

            entities.add(new Item());

            //For each record ignore the name and input the
            //value in record array
            getRecord(record, input);


            
            attrItr = 0;
            entities.get(itemItr).setID(record[attrItr++]);
            entities.get(itemItr).setName(record[attrItr++]);
            entities.get(itemItr).setType(record[attrItr++]);
            entities.get(itemItr).setMin(record[attrItr++]);
            entities.get(itemItr).setMax(record[attrItr++]);
            entities.get(itemItr).setSpeed(record[attrItr++]);

            //loop through attributes to change attributes
            for(Attributes attr: Attributes.values()) {
                int setAttr = Integer.parseInt(record[attrItr]);
                (entities.get(itemItr)).setAttribute(attr,setAttr);
                attrItr++;
            }
            itemItr++;
            if(!input.hasNext())
                done = true;
        }


        input.close();
        return true;
    }

    public void getRecord(String[] record, Scanner input) {
        String temp1 = " ";
        int bracketLoc = 0;
        int recordStart = 0;
        for(int i = 0; i < RECORD_LENGTH; i++) {
            temp1 = input.nextLine();
            bracketLoc = temp1.indexOf(']');
            for(int j = bracketLoc + 1; j < temp1.length();j++) {
                if(!(temp1.charAt(j) == ' ')) {
                    recordStart = j;
                    break;
                }
            }
            record[i] = temp1.substring(recordStart, temp1.length());
        }
        //test
        for(int i = 0; i < RECORD_LENGTH; i++) {
            System.out.println(record[i]);
        }
    }
}
